package com.wanhe.shoppingcart.counter;

import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.holder.DishHolder;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.shoppingcart.manager.ICartItem;

/**
 * Created by XieZaiWei on 2017/5/9.
 * 最少4份菜的加减
 */

public class AddFourTimeCounter extends BaseCustomCounter implements ICounter {

	public static final String COUNTER_TYPE = "four_times";

	public AddFourTimeCounter(BaseCounter counter) {
		super(counter);
	}

	@Override
	public String getCounterType() {
		return COUNTER_TYPE;
	}

	@Override
	public void plus(String dishId, boolean isNeedSyncToServer) {
		//一次点击后，需要直接添加4个
		Dish dish = DishHolder.getInstance().get(dishId);
		if (ObjectUtil.checkObject(dish)) {
			int count = CartManager.getInstance().getSameGuidItemCount(dishId);
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setCount(count);
			plus(cartItem, isNeedSyncToServer);
		}
	}

	@Override
	public void plus(ICartItem item, boolean isNeedSyncToServer) {
		int count = item.getCount();
		if (count == 0) {
			item.setCount(4);
		}
		mBaseCounter.plus(item, isNeedSyncToServer);
	}

	@Override
	public void reduce(String dishUuid, boolean isNeedSyncToServer) {
		//每次点击减少一个，但是到达数量为4的时候，再点击就是直接删除该购物项
		Dish dish = DishHolder.getInstance().get(dishUuid);
		if (ObjectUtil.checkObject(dish)) {
			int count = CartManager.getInstance().getSameGuidItemCount(dishUuid);
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setCount(count);
			reduce(cartItem, isNeedSyncToServer);
		}
	}


	@Override
	public void reduce(ICartItem item, boolean isNeedSyncToServer) {
		int count = item.getCount();
		if (count <= 4) {
			CartManager.getInstance().deleteItem(item);
		} else {
			CartManager.getInstance().reduceItem(item.getDishGuid(), count, isNeedSyncToServer);

		}
	}

	@Override
	public boolean isCountFilled() {
		return true;
	}
}
